﻿using Microsoft.IdentityModel.Tokens;

namespace DataAcquisition
{
    public partial class AddProjectForm : BaseForm
    {
        #region 属性
        /// <summary>
        /// 试压工程
        /// </summary>
        private Project obj = new Project();

        /// <summary>
        /// 试压模板
        /// </summary>
        public TemplateItem? templateObj;

        #endregion

        #region 自定义方法
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = NumberInput.Text;
            List<Project> list = SqlHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】编号工程已存在", where["number"]);
                Error(msg);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>加载模板信息
        /// 
        /// </summary>
        public void LoadTemplateInfo()
        {
            if (templateObj is not null)
            {
                bool res = CheckTemplateExist(templateObj.Number);
                if (res)
                {
                    DesignPressureInput.Text = templateObj.DesignPressure.ToString();
                    if (templateObj.PipePosition is not null)
                    {
                        if (templateObj.PipePosition.Contains("站内/集输"))
                        {
                            TestTypeSelect.Text = "站内/集输";
                        }
                        else if (templateObj.PipePosition.Contains("站内"))
                        {
                            TestTypeSelect.Text = "站内";
                        }
                        else if (templateObj.PipePosition.Contains("集输"))
                        {
                            TestTypeSelect.Text = "集输";
                        }
                    }
                }
            }
            else
            {
                string msg = string.Format("请选择试压模板");
                Error(msg);
            }
            this.DesignPressureInput.Enabled = true;
        }

        /// <summary>判断模板是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckTemplateExist(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Template> list = SqlHelper.Inquireable<Template>(where);
            if (list.Count == 0)
            {
                string msg = string.Format("【{0}】编号模板不存在", number);
                Error(msg);
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        #region 系统方法
        public AddProjectForm()
        {
            InitializeComponent();
            Header.Text = "添加工程";
        }
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (NumberInput.Text.IsNullOrEmpty())
            {
                Error("请输入工程编号");
                NumberInput.Focus();
                return;
            }
            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.Number = NumberInput.Text;

            if (NameInput.Text.IsNullOrEmpty())
            {
                Error("请输入工程名称");
                NameInput.Focus();
                return;
            }
            obj.Name = NameInput.Text;

            if (StakeMarkInput.Text.IsNullOrEmpty())
            {
                Error("请输入工程桩号");
                StakeMarkInput.Focus();
                return;
            }
            obj.StakeMark = StakeMarkInput.Text;

            if (PipeSpecificationInput.Text.IsNullOrEmpty())
            {
                Error("请输入管材规格");
                PipeSpecificationInput.Focus();
                return;
            }
            obj.PipeSpecification = PipeSpecificationInput.Text;

            if (PipeLengthInput.Text.IsNullOrEmpty())
            {
                Error("请输入管材长度");
                PipeLengthInput.Focus();
                return;
            }
            obj.PipeLength = Math.Round(double.Parse(PipeLengthInput.Text), 3);

            if (MediumInput.Text.IsNullOrEmpty())
            {
                Error("请输入试压介质");
                MediumInput.Focus();
                return;
            }
            obj.Medium = MediumInput.Text;

            if (templateObj == null)
            {
                Error("请选择试压模板");
                return;
            }
            //obj.DesignPressure = Math.Round(double.Parse())

            if (PipeNatureSelect.Text.IsNullOrEmpty())
            {
                Error("请选择管道性质");
                return;
            }
            obj.PipeNature = PipeNatureSelect.Text;

            if (TestTypeSelect.Text.IsNullOrEmpty())
            {
                Error("请选择试压类型");
                return;
            }
            obj.TestType = TestTypeSelect.Text;

            if (CompanyInput.Text.IsNullOrEmpty())
            {
                Error("请输入施工单位");
                CompanyInput.Focus();
                return;
            }
            obj.Company = CompanyInput.Text;

            if (ManometerPrecisionSelect.Text.IsNullOrEmpty())
            {
                Error("请选择压力表精度");
                return;
            }
            obj.ManometerPrecision = ManometerPrecisionSelect.Text;

            if (ManometerRangeSelect.Text.IsNullOrEmpty())
            {
                Error("请选择压力表量程");
                return;
            }
            obj.ManometerRange = ManometerRangeSelect.Text;

            obj.DesignPressure = Math.Round(double.Parse(DesignPressureInput.Text),3);
            obj.TemplateID = templateObj.ID;

            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlHelper.Insertable(list);
            if (res > 0)
            {
                this.Close();
            }
            else
            {
                Error("添加工程失败");
            }
        }

        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            DesignPressureInput.Enabled = false;
            SelectTemplateForm frm = new SelectTemplateForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        #endregion
    }
}
